import React from 'react';
import { Button, message } from 'antd';
import { WeaTools, WeaLocaleProvider, WeaTop, WeaDialog, WeaFormItem, WeaInput, WeaUpload } from 'ecCom';
import Message from './Message';

const getLabel = WeaLocaleProvider.getLabel;

class WeaLicenseSubmit extends React.Component {
  constructor(props) {
    super(props);
    this.state = { loading: false, licenseCode: '', data: {}, isChangeCode: false, isShowMsg: false, message: '' };
  }

  componentWillMount() {
    const { display, visible } = this.props;
    if (display == 'page' || visible) {
      this.onLoad();
    }
  }

  componentWillReceiveProps(nextProps) {
    const { visible } = nextProps;
    if (visible) {
      this.onLoad();
    }
  }

  render() {
    const { display, visible } = this.props;
    const { loading, licenseCode = '', data = {}, isChangeCode, isShowMsg, message } = this.state;
    const { code = '', code_old = '', code_new = '', code_rep = '' } = data;

    const colProps = { labelCol: { span: 6 }, wrapperCol: { span: 18 } };

    let Content = (
      <div className="wea-license-form-group">
        <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@b4pdsm`} label={getLabel(22910, '验证码')} {...colProps}>
          <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@g5lp60`}
            style={{ width: '270px' }}
            type="password"
            value={code}
            viewAttr={3}
            onChange={value => this.setState({ data: { ...data, code: value } })}
          />
          <span className="wea-license-form-change" onClick={() => this.setState({ isChangeCode: true })}>
            {getLabel(517208, '更改验证码')}
          </span>
        </WeaFormItem>
        <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@y3kpud`} label={`license${getLabel(18493, '文件')}`} {...colProps}>
          <WeaUpload ecId={`${this && this.props && this.props.ecId || ''}_WeaUpload@gigo4f`}
            ref="WeaUpload"
            uploadId="LicenseSubmit"
            uploadUrl="/api/system/license/LicenseOperation"
            uploadParams={{ code }}
            category="string"
            clearWhenReset={false}
            multiSelection={false}
            maxFilesNumber={1}
            showClearAll={false}
            autoUpload={false}
            limitType="license"
            viewAttr={3}
            onChange={(ids, list, args) => this.setState({ isShowMsg: true, message: args[0].message })}
          />
        </WeaFormItem>
        <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@drxsao`} label={getLabel(15019, '识别码')} {...colProps} underline>
          <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@vapfav`} value={licenseCode} viewAttr={1} />
        </WeaFormItem>
        <div className="wea-license-tip-msg">
          <i className="icon-coms02-Warning-01" />
          <span style={{ marginLeft: '10px' }}>{getLabel(517211, '提示：公司名称及识别码提交给软件供应商，以获取License')}</span>
        </div>
      </div>
    );

    if (isChangeCode) {
      Content = (
        <div className="wea-license-form-group">
          <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@kblhsh`} label={getLabel(517209, '旧验证码')} {...colProps}>
            <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@28jngv`} type="password" value={code_old} viewAttr={3} onChange={value => this.setState({ data: { ...data, code_old: value } })} />
          </WeaFormItem>
          <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@mlzj7b`} label={getLabel(22910, '验证码')} {...colProps}>
            <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@qptxci`} type="password" value={code_new} viewAttr={3} onChange={value => this.setState({ data: { ...data, code_new: value } })} />
          </WeaFormItem>
          <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@nzdjpv`} label={getLabel(517210, '确认验证码')} {...colProps}>
            <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@ckyzm2`} type="password" value={code_rep} viewAttr={3} onChange={value => this.setState({ data: { ...data, code_rep: value } })} />
          </WeaFormItem>
        </div>
      );
    }

    if (isShowMsg) {
      Content = <Message ecId={`${this && this.props && this.props.ecId || ''}_Message@pekk1a`} message={message} />;
    }

    if (display == 'page') {
      return (
        <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@vog6bp`}
          loading={loading}
          title={`License${getLabel(615, '提交')}`}
          icon={<i className="icon-portal-license" />}
          iconBgcolor="#a7adb5"
          buttons={this.getButtons()}
        >
          {Content}
        </WeaTop>
      );
    }

    return (
      <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@439b1i`}
        visible={visible}
        loading={loading}
        title={`License${getLabel(615, '提交')}`}
        icon="icon-portal-license"
        iconBgcolor="#a7adb5"
        style={{ width: 600 }}
        zIndex={999}
        hasScroll={true}
        buttons={this.getButtons()}
        onCancel={this.onCancel}
      >
        {Content}
      </WeaDialog>
    );
  }

  getButtons = () => {
    const { display, from } = this.props;
    const { isShowMsg, message, isChangeCode } = this.state;

    const buttons = [];
    if (isChangeCode) {
      buttons.push(
        <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@ea5jo1`} type="primary" onClick={this.onCodeSubmit}>
          {getLabel(615, '提交')}
        </Button>,
      );
      buttons.push(
        <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@5evw8u`} type="primary" onClick={this.onBack}>
          {getLabel(31129, '取消')}
        </Button>,
      );
    } else if (isShowMsg) {
        if (message == '1') {
          if (display == 'page') {
            buttons.push(
              <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@n8qmys`} type="primary" onClick={() => (window.location.href = '/wui/index.html')}>
                {getLabel(674, '登录')}
              </Button>,
            );
          } else {
            buttons.push(
              <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@3i6nsl`} type="primary" onClick={this.onCancel}>
                {from == 'login' ? getLabel(674, '登录') : getLabel(309, '关闭')}
              </Button>,
            );
          }
        } else {
          buttons.push(
            <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@sxxee7`} type="primary" onClick={this.onBack}>
              {getLabel(1290, '返回')}
            </Button>,
          );
        }
      } else {
        buttons.push(
          <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@mz8m6v`} type="primary" onClick={this.onSubmit}>
            {getLabel(615, '提交')}
          </Button>,
        );
      }
    return buttons;
  };

  onLoad = () => {
    this.setState({ loading: true });
    WeaTools.callApi('/api/system/license/InLicense', 'POST').then((result) => {
      this.setState({ loading: false, licenseCode: result.licenseCode });
    });
  };

  onSubmit = () => {
    const { data = {} } = this.state;
    const { code = '' } = data;
    if (code.trim() != '') {
      this.refs.WeaUpload.doUpload();
    } else {
      message.warning(getLabel(384134, '必要信息不完整！'));
    }
  };

  onCodeSubmit = () => {
    const { data = {} } = this.state;
    const { code_old = '', code_new = '', code_rep = '' } = data;

    if (code_old.trim() != '' && code_new.trim() != '' && code_rep.trim() != '') {
      if (code_new === code_rep) {
        this.setState({ loading: true });
        WeaTools.callApi('/api/system/license/CodeOperation', 'POST', { passwordold: code_old, passwordnew: code_new }).then((result) => {
          const msg = result.message;
          if (msg == '1') {
            message.error(getLabel(128878, '验证码有误'));
          } else {
            message.success(msg);
            this.onBack();
          }
          this.setState({ loading: false });
        });
      } else {
        message.warning(getLabel(128878, '验证码有误'));
      }
    } else {
      message.warning(getLabel(384134, '必要信息不完整！'));
    }
  };

  onBack = () => {
    this.setState({ data: {}, isShowMsg: false, isChangeCode: false });
  };

  onCancel = () => {
    this.props.onHide();
    this.setState({ data: {}, isShowMsg: false, isChangeCode: false });
  };
}

export default WeaLicenseSubmit;
